package com.toubutu.audio.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.toubutu.audio.admin.dto.RolePermissionBindDto;
import com.toubutu.audio.admin.entity.RolePermission;
import com.toubutu.audio.admin.mapper.RolePermissionMapper;
import com.toubutu.audio.admin.service.RolePermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戒燥
 * @since 2023-09-30
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public Boolean deleteRolePermissionByRoleId(Integer roleId) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId,roleId);
        return this.remove(wrapper);
    }

    @Override
    public Boolean deletePermissionRoleByPermissionIds(List<Integer> menuIds) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(RolePermission::getPermissionId,menuIds);
        return this.remove(wrapper);
    }

    @Override
    public void saveBatch(RolePermissionBindDto bindDto) {
        rolePermissionMapper.saveBatch(bindDto);
    }

    @Override
    public List<Integer> getPermissionIdsByRoleId(Integer roleId) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId,roleId);
        wrapper.select(RolePermission::getPermissionId);
        return this.list(wrapper).stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
    }

    @Override
    public List<String> getPermissionCodeByRoleList(List<Integer> roleIds) {
        if (roleIds.isEmpty()){
            return Collections.emptyList();
        }
        return rolePermissionMapper.queryPermissionCode(roleIds);
    }
}
